package com.ruoyi.driver.service.impl;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import com.alibaba.nacos.common.utils.IoUtils;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.ruoyi.common.core.utils.file.FileTypeUtils;
import com.ruoyi.driver.domain.DrivingLicenseInfo;
import com.ruoyi.driver.domain.IdCardInfo;
import com.ruoyi.driver.domain.VehicleLicenseInfo;
import com.ruoyi.driver.ocrUtil.OcrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.driver.mapper.DriversMapper;
import com.ruoyi.driver.domain.Drivers;
import com.ruoyi.driver.service.IDriversService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 司机基本信息Service业务层处理
 * 
 * @author yyj
 * @date 2025-08-17
 */
@Service
public class DriversServiceImpl implements IDriversService 
{
    @Autowired
    private DriversMapper driversMapper;

    /**
     * 域名或本机访问地址
     */
    @Value("${fdfs.domain}")
    public String domain;

    @Autowired
    private FastFileStorageClient storageClient;

    /**
     * 查询司机基本信息
     * 
     * @param id 司机基本信息主键
     * @return 司机基本信息
     */
    @Override
    public Drivers selectDriversById(Long id)
    {
        return driversMapper.selectDriversById(id);
    }

    /**
     * 查询司机基本信息列表
     * 
     * @param drivers 司机基本信息
     * @return 司机基本信息
     */
    @Override
    public List<Drivers> selectDriversList(Drivers drivers)
    {
        return driversMapper.selectDriversList(drivers);
    }

    /**
     * 新增司机基本信息
     * 
     * @param drivers 司机基本信息
     * @return 结果
     */
    @Override
    public int insertDrivers(Drivers drivers)
    {
        return driversMapper.insertDrivers(drivers);
    }

    /**
     * 修改司机基本信息
     * 
     * @param drivers 司机基本信息
     * @return 结果
     */
    @Override
    public int updateDrivers(Drivers drivers)
    {
        return driversMapper.updateDrivers(drivers);
    }

    /**
     * 批量删除司机基本信息
     * 
     * @param ids 需要删除的司机基本信息主键
     * @return 结果
     */
    @Override
    public int deleteDriversByIds(Long[] ids)
    {
        return driversMapper.deleteDriversByIds(ids);
    }

    /**
     * 删除司机基本信息信息
     * 
     * @param id 司机基本信息主键
     * @return 结果
     */
    @Override
    public int deleteDriversById(Long id)
    {
        return driversMapper.deleteDriversById(id);
    }

    @Override
    public Drivers selectDriversByPhone(String phone) {
        return driversMapper.selectDriversByPhone(phone);
    }

    /**
     * 上传身份证
     * @param file
     * @return
     */
    @Override
    public HashMap<String, Object> uploadIdCard(MultipartFile file) {
        InputStream inputStream = null;
        try
        {
            inputStream = file.getInputStream();
            StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(), FileTypeUtils.getExtension(file), null);
            String url = domain + "/" + storePath.getFullPath();
            HashMap<String, Object> map = new HashMap<>();
            String resultMsg = OcrUtil.ocrIdentification(1,file);
            IdCardInfo idCardInfo = OcrUtil.parseIdCardInfo(resultMsg);
            map.put("url",url);
            map.put("idCardInfo",idCardInfo);
            return map;
        }
        catch (Exception e)
        {
            throw new RuntimeException("FastDfs Failed to upload file", e);
        }
        finally
        {
            IoUtils.closeQuietly(inputStream);
        }
    }

    /**
     * 上传驾驶证
     * @param file
     * @return
     */
    @Override
    public HashMap<String, Object> uploadDrivingLicence(MultipartFile file) {
        InputStream inputStream = null;
        try
        {
            inputStream = file.getInputStream();
            StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(), FileTypeUtils.getExtension(file), null);
            String url = domain + "/" + storePath.getFullPath();
            HashMap<String, Object> map = new HashMap<>();
            String resultMsg = OcrUtil.ocrIdentification(2,file);
            DrivingLicenseInfo drivingLicenseInfo = OcrUtil.parseDrivingLicenseInfo(resultMsg);
            map.put("url",url);
            map.put("drivingLicenseInfo",drivingLicenseInfo);
            return map;
        }
        catch (Exception e)
        {
            throw new RuntimeException("FastDfs Failed to upload file", e);
        }
        finally
        {
            IoUtils.closeQuietly(inputStream);
        }
    }

    /**
     * 上传行驶证
     * @param file
     * @return
     */
    @Override
    public HashMap<String, Object> uploadVehicleLicense(MultipartFile file) {
        InputStream inputStream = null;
        try
        {
            inputStream = file.getInputStream();
            StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(), FileTypeUtils.getExtension(file), null);
            String url = domain + "/" + storePath.getFullPath();
            HashMap<String, Object> map = new HashMap<>();
            String resultMsg = OcrUtil.ocrIdentification(3,file);
            VehicleLicenseInfo vehicleLicenseInfo = OcrUtil.parseVehicleLicenseInfo(resultMsg);
            map.put("url",url);
            map.put("vehicleLicenseInfo",vehicleLicenseInfo);
            return map;
        }
        catch (Exception e)
        {
            throw new RuntimeException("FastDfs Failed to upload file", e);
        }
        finally
        {
            IoUtils.closeQuietly(inputStream);
        }
    }
}
